﻿
// ProjectAADlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ProjectAA.h"
#include "ProjectAADlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//ImgCap *imgCap;
//ImgFile imgFile; 
//CTS CTSCam;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

	// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CProjectAADlg 对话框



CProjectAADlg::CProjectAADlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROJECTAA_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProjectAADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_100, Static1);
	DDX_Control(pDX, IDC_BUTTON_HOME4, Static2);
	DDX_Control(pDX, IDC_STATIC_101, Static3);
	DDX_Control(pDX, IDC_BUTTON1, Static4);
	DDX_Control(pDX, IDC_STATIC_60, Static5);
	DDX_Control(pDX, IDC_STATIC_66, Static6);
	DDX_Control(pDX, IDC_STATIC1, Static7);
	DDX_Control(pDX, IDC_STATIC_62, Static8);
	DDX_Control(pDX, IDC_BUTTON_HOME5, Static9);
	DDX_Control(pDX, IDC_STATIC2, Static10);
	DDX_Control(pDX, IDC_STATIC_68, Static11);
	DDX_Control(pDX, IDC_STATIC3, Static12);
	DDX_Control(pDX, IDC_STATIC_96, Static13);
	DDX_Control(pDX, IDC_STATIC_2, Static14);
	DDX_Control(pDX, IDC_STATIC_1, Static15);
	DDX_Control(pDX, IDC_STATIC_51, Static16);
	DDX_Control(pDX, IDC_STATIC_10SN, Static17);
	DDX_Control(pDX, IDC_CHECK_10AUTO2, Button1);
	DDX_Control(pDX, IDC_CHECK_10AUTO, Button2);
	DDX_Control(pDX, IDC_BUTTON_HOME, Button3);
	DDX_Control(pDX, IDC_STATIC_10STATUS, Msg1);
	DDX_Control(pDX, IDC_STATIC_20STATUS2, Msg2);
	DDX_Control(pDX, IDC_STATIC_10ERROR, Msg3);
	DDX_Control(pDX, IDC_STATIC_20ERROR2, Msg4);
	DDX_Control(pDX, IDC_STATIC4, Static19);
	DDX_Control(pDX, IDC_BUTTON_START, m_butStart);
	DDX_Control(pDX, IDC_BUTTON_STOP, m_butStop);
	DDX_Control(pDX, IDC_BUTTON_SAVE, m_butSave);
	DDX_Control(pDX, IDC_STATIC_102, Static20);
	DDX_Control(pDX, IDC_STATIC_97, Static21);
	DDX_Control(pDX, IDC_STATIC_10STATUS2, NGMsg);
}

BEGIN_MESSAGE_MAP(CProjectAADlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_32771, &CProjectAADlg::OnOpenLogDlg)
	ON_COMMAND(ID_32773, &CProjectAADlg::OnOpenMotorDlg)
	ON_WM_SIZE()
	ON_COMMAND(ID_32774, &CProjectAADlg::OnOpenTcpipDlg)
	ON_MESSAGE(WM_SEND_SOCKET_SERVER_MSG_TO_MAIN_UI, &CProjectAADlg::OnReceiveServerMsg)
	ON_WM_CTLCOLOR()
	ON_COMMAND(ID_32783, &CProjectAADlg::OnOpenProjectDlg)
	ON_COMMAND(ID_32784, &CProjectAADlg::OnOpenIODlg)
	ON_BN_CLICKED(IDC_BUTTON_HOME, &CProjectAADlg::OnBnClickedButtonHome)
	ON_BN_CLICKED(IDC_CHECK_10AUTO, &CProjectAADlg::OnBnClickedCheck10auto)
	ON_COMMAND(ID_32785, &CProjectAADlg::OnOpenControlDlg)
	ON_COMMAND(ID_32786, &CProjectAADlg::OnOpenSettingDlg)
	ON_COMMAND(ID_32787, &CProjectAADlg::OnOpenRS232Dlg)
	ON_BN_CLICKED(IDC_CHECK_10AUTO2, &CProjectAADlg::OnBnClickedCheck10auto2)
	ON_BN_CLICKED(IDC_BUTTON_HOME4, &CProjectAADlg::OnBnClickedButtonHome4)
	ON_BN_CLICKED(IDC_BUTTON1, &CProjectAADlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON_HOME5, &CProjectAADlg::OnBnClickedButtonHome5)
	ON_COMMAND(ID_32788, &CProjectAADlg::OnOpenLoginDlg)
	ON_COMMAND(ID_32789, &CProjectAADlg::OnOpenCaptureDlg)
	ON_BN_CLICKED(IDC_BUTTON_START, &CProjectAADlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CProjectAADlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CProjectAADlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON2, &CProjectAADlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CProjectAADlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CProjectAADlg::OnBnClickedButton4)
	ON_COMMAND(ID_32790, &CProjectAADlg::OnOpenGlueCheckDlg)
	ON_COMMAND(ID_MENU_MES, &CProjectAADlg::OnMenuMes)
END_MESSAGE_MAP()


// CProjectAADlg 消息处理程序

BOOL CProjectAADlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	unique_mutex = CreateMutex(NULL, FALSE, L"Project_10");
	DWORD errCode = GetLastError();
	if (errCode == ERROR_ALREADY_EXISTS) {
		Sleep(500);
		CloseHandle(unique_mutex);
		MessageBox(L"10站软件已经被打开!", L"Warning", 0);
		::TerminateProcess(::GetCurrentProcess(), NULL);//终止原先程序
	}

	// 设置字体参数
	CFont	m_editFont;//字体
	m_editFont.CreateFont(40, 0, 4, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));
	Msg1.SetFont(&m_editFont);
	Msg2.SetFont(&m_editFont);
	Msg3.SetFont(&m_editFont);
	Msg4.SetFont(&m_editFont);
	NGMsg.SetFont(&m_editFont);
	Button1.SetFont(&m_editFont); //
	Button2.SetFont(&m_editFont); //
	Button3.SetFont(&m_editFont); //

	//m_editFont.CreateFontW(26, 10, 4, 0, 500, FALSE, FALSE, 0,
	//	ANSI_CHARSET,
	//	OUT_DEFAULT_PRECIS,
	//	CLIP_DEFAULT_PRECIS,
	//	DEFAULT_QUALITY,
	//	DEFAULT_PITCH | FF_SWISS,
	//	_T("Arial"));
	//Static1.SetFont(&m_editFont); //
	//Static2.SetFont(&m_editFont); //
	//Static3.SetFont(&m_editFont); //
	//Static4.SetFont(&m_editFont); //
	//Static5.SetFont(&m_editFont); //
	//Static6.SetFont(&m_editFont); //
	//Static7.SetFont(&m_editFont); //
	//Static8.SetFont(&m_editFont); //
	//Static9.SetFont(&m_editFont); //
	//Static10.SetFont(&m_editFont); //
	//Static11.SetFont(&m_editFont); //
	//Static12.SetFont(&m_editFont); //
	//Static13.SetFont(&m_editFont); //
	//Static14.SetFont(&m_editFont); //
	//Static15.SetFont(&m_editFont); //
	//Static16.SetFont(&m_editFont); //
	//Static17.SetFont(&m_editFont); //
	//Static19.SetFont(&m_editFont); //
	//Static20.SetFont(&m_editFont); //
	//Static21.SetFont(&m_editFont); //

	m_editFont.CreateFont(26, 0, 8, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));
	//轴的比率尺
	//gSystemMgr.m_vCMotor[MOTOR_Z].PositionRatio = 284.44;//3		102400/360

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	gSystemMgr.GeneralSettingIniInit();//读取型号配置

	m_menu.LoadMenuW(IDR_MENU1);
	this->SetMenu(&m_menu);

	SetUiHandle();

	gSystemMgr.CaptureConfigIniInit();//采图配置
	//gSystemMgr.TcpipConfigIniInit();//Tcpip通讯配置
	gSystemMgr.ReadErrorListIni();//错误信息
	gSystemMgr.PosConfigIniInit();//位置配置
	gSystemMgr.RS232ConfigIniInit();
	gSystemMgr.LoginConfigIniInit();
	gSystemMgr.GlueCheckConfigIniInit();
	gSystemMgr.MesConfigIniInit();

	gSystemMgr.m_ptrMainUI = this;

	if (m_cLogDlg == nullptr)
	{
		m_cLogDlg = new CLogDlg;
		m_cLogDlg->Create(IDD_DIALOG_LOG, this);
	}
	m_cLogDlg->ShowWindow(SW_HIDE);

	bool bInit = gSystemMgr.MotorIO_Init();//轴卡初始化
	if (bInit)
	{
		gSystemMgr.StartGetMotorEncoderThread();
		//gSystemMgr.m_cIo.SetAllOutputOff();
		gSystemMgr.m_cIo.StartGetIoInputThread();
		gSystemMgr.m_cIo.SetOutputIo(35, ON);
		Sleep(1000);
		gSystemMgr.m_cIo.SetOutputIo(34, ON);
		gSystemMgr.m_cIo.SetOutputIo(36, ON);
	}


	switch (gSystemMgr.m_nImageSource)
	{
		case IMG_DEV_FILE:
			gSystemMgr.imgCap = (ImgCap*)&gSystemMgr.imgFile;
			//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"Image Files");
			break;
		case IMG_DEV_CTS:
			Cam_Init();
			gSystemMgr.m_bCTSInitFlag = 1;
			gSystemMgr.imgCap = (ImgCap*)&gSystemMgr.imgCTS;
			//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"CTS Cam");
			break;
		case IMG_RO_LONGO:
			gSystemMgr.imgCap = (ImgCap*)&gSystemMgr.imgRLG;
			//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"RLG Cam");
			break;
	}
	gSystemMgr.StartCaptureThread();
	gSystemMgr.StartImageTestThread();

	GetClientRect(&m_rcUiRect);

	//Tcpip通信自动建立服务器 或 自动连接
	//if (gSystemMgr.m_nModeSelect == 0 && gSystemMgr.m_bAutoFlag) {
	//	USES_CONVERSION;
	//	char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd);
	//	bool bCreateServer = gSystemMgr.m_cSocketServer.OpenSocketServer("Server", c_strIp, gSystemMgr.m_nPort);
	//	CString cstr;
	//	cstr.Format(L"Socket Server Build result:%d", bCreateServer);
	//	gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR); 
	//	if (bCreateServer)
	//	{
	//		gSystemMgr.StartSocketAcceptClientThread();
	//	}
	//}

	//gSystemMgr.StartTcpipRelinkThread();
	gSystemMgr.StartScanButtonValueThread();
	gSystemMgr.StartOtherThread();
	gSystemMgr.StartAutoThread();
	gSystemMgr.StartLightControlThread();
	gSystemMgr.StartRS232LinkThread();

	//画按钮
	CRect rect;
	m_butStart.GetWindowRect(rect);
	long width = rect.right - rect.left;
	long height = rect.bottom - rect.top;
	HICON hicon_btn = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_START), IMAGE_ICON, width - 30, height - 10, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION);
	m_butStart.SetIcon(hicon_btn);
	m_butStop.GetWindowRect(rect);
	width = rect.right - rect.left;
	height = rect.bottom - rect.top;
	hicon_btn = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_STOP), IMAGE_ICON, width - 30, height - 10, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION);
	m_butStop.SetIcon(hicon_btn);
	m_butSave.GetWindowRect(rect);
	width = rect.right - rect.left;
	height = rect.bottom - rect.top;
	hicon_btn = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_SAVE), IMAGE_ICON, width - 30, height - 10, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION);
	m_butSave.SetIcon(hicon_btn);

	////更新显示
	//int m_nCrtScreenWidth = 0;
	//int m_nCrtScreenHeight = 0;
	//CRect rcScreen;
	//SystemParametersInfo(SPI_GETWORKAREA, 0, &rcScreen, 0);
	//m_nCrtScreenWidth = rcScreen.Width() / 1;
	//m_nCrtScreenHeight = rcScreen.Height() / 1;
	
	ShowWindow(SW_MAXIMIZE);

	SetWindowText(L"Shengtaike_10 L3 S10 v1.1.5 [#D]");

	return TRUE;
}

void CProjectAADlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

void CProjectAADlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CRect rect;
		CPaintDC dc(this);
		GetClientRect(rect);
		dc.FillSolidRect(rect, RGB(169, 179, 195));//设置为绿色背景  

		CDialogEx::OnPaint();
	}
}

HCURSOR CProjectAADlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CProjectAADlg::SetUiHandle()
{
	//m_menu.EnableMenuItem(ID_32774, MF_DISABLED);
	//m_menu.EnableMenuItem(ID_32773, MF_DISABLED);
	//m_menu.EnableMenuItem(ID_32786, MF_DISABLED);
	//m_menu.EnableMenuItem(ID_32787, MF_DISABLED);
	//m_menu.EnableMenuItem(ID_32784, MF_DISABLED);
	//m_menu.EnableMenuItem(ID_32785, MF_DISABLED);
	//m_menu.EnableMenuItem(ID_32789, MF_DISABLED);
	//m_menu.EnableMenuItem(ID_MENU_MES, MF_DISABLED);

}


void CProjectAADlg::OnOpenLogDlg()
{
	if (m_cLogDlg != nullptr)
	{
		m_cLogDlg->ShowWindow(SW_SHOW);
	}
}


void CProjectAADlg::OnOpenMotorDlg()
{
	if (m_cMotorDlg != nullptr)
	{
		delete m_cMotorDlg;
		m_cMotorDlg = nullptr;
	}
	if (m_cMotorDlg == nullptr)
	{
		m_cMotorDlg = new CMotorDlg;
		m_cMotorDlg->Create(IDD_DIALOG_MOTOR, this);
	}
	m_cMotorDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::Repaint(CWnd* pWnd, int last_Width, int now_Width, int last_Height, int now_Height, int cnt)
{
	if (pWnd)
	{
		CRect rect;
		CRect rect2;
		pWnd->GetWindowRect(&rect2);
		rect = OriRect[cnt];
		int n = 62;
		int m = 8;
		//rect2.top = rect2.top - n;
		//rect2.bottom = rect2.bottom - n;
		ScreenToClient(&rect2);

		double proportion_x = now_Width / (double)last_Width;
		double proportion_y = now_Height / (double)last_Height;

		rect.left = (long)(rect.left * proportion_x + 0.5 - m * proportion_x);
		rect.right = (long)(rect.left + RectW[cnt] * proportion_x + 0.5);
		rect.top = (long)(rect.top * proportion_y + 0.5 - n * proportion_y);
		rect.bottom = (long)(rect.top + RectH[cnt] * proportion_y + 0.5);
		pWnd->MoveWindow(&rect);
	}

}

void CProjectAADlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED)//窗体大小发生变动。处理程序
	{
		float fsp[2];
		POINT Newp; //获取现在对话框的大小
		CRect recta;
		GetClientRect(&recta); //取客户区大小   
		Newp.x = recta.right - recta.left;
		Newp.y = recta.bottom - recta.top;
		fsp[0] = (float)Newp.x / Old.x;
		fsp[1] = (float)Newp.y / Old.y;
		CRect Rect;
		int woc;
		CPoint OldTLPoint, TLPoint; //左上角
		CPoint OldBRPoint, BRPoint; //右下角
		HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); //列出所有控件   
		while (hwndChild)
		{
			woc = ::GetDlgCtrlID(hwndChild);//取得ID
			GetDlgItem(woc)->GetWindowRect(Rect);
			ScreenToClient(Rect);
			OldTLPoint = Rect.TopLeft();
			TLPoint.x = long(OldTLPoint.x * fsp[0]);
			TLPoint.y = long(OldTLPoint.y * fsp[1]);
			OldBRPoint = Rect.BottomRight();
			BRPoint.x = long(OldBRPoint.x * fsp[0]);
			BRPoint.y = long(OldBRPoint.y * fsp[1]); //高度不可读的控件（如:combBox),不要改变此值.
			Rect.SetRect(TLPoint, BRPoint);
			GetDlgItem(woc)->MoveWindow(Rect, TRUE);
			hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
		}
		Old = Newp;
	}
	//GetClientRect(&m_rcUiRect); //save size of dialog
	Invalidate();//更新窗口
}


void CProjectAADlg::OnOpenTcpipDlg()
{
	//if (m_cTcpipDlg != nullptr)
	//{
	//	delete m_cTcpipDlg;
	//	m_cTcpipDlg = nullptr;
	//}
	//if (m_cTcpipDlg == nullptr)
	//{
	//	m_cTcpipDlg = new CTcpipDlg;
	//	m_cTcpipDlg->Create(IDD_DIALOG_TCPIP, this);
	//}
	//m_cTcpipDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnCancel()
{
	gSystemLog.DisplayLogAndSave(L"关闭软件", LV_ERROR);
	Sleep(100);
	gSystemMgr.m_nExitFlag = 1;
	delete m_cMotorDlg;
	delete m_cLogDlg;
	delete m_cProjectDlg;
	dmc_emg_stop(gSystemMgr.m_nCard);
	gSystemMgr.m_vCMotor[MOTOR_Z1].ServoOff();
	Sleep(500);
	if (gSystemMgr.ScalesConnectFlag)
		gSystemMgr.m_cScalesComm.com_close();
	gSystemMgr.m_cPlasmaComm.com_close();
	gSystemMgr.m_cRfidComm1.com_close();
	gSystemMgr.m_cRfidComm2.com_close();
	dmc_board_close();//释放资源m_cRfidComm1
	CDialogEx::OnCancel();
}

LRESULT CProjectAADlg::OnReceiveServerMsg(WPARAM wParam, LPARAM lParam)
{
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);

	gSystemLog.DisplayLogAndSave(cstrp.GetBuffer(), LV_EVENT);

	std::vector<wstring> vResultItem = _utils::SplitString(cstrp.GetBuffer(), L",");

	if (vResultItem.size() < 2)
	{
		gSystemLog.DisplayLogAndSave(L"Client 返回的数据有误", LV_ERROR);
		return LRESULT();
	}

	return LRESULT();
}
HBRUSH CProjectAADlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr1 = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	DeleteObject(hbr);

	UINT id = pWnd->GetDlgCtrlID();
	CString Cstr;
	if (id == IDC_CHECK_10AUTO)
	{
		if (gSystemMgr.m_bAutoFlag_20) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetTextColor(RGB(0, 255, 0));
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetTextColor(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_CHECK_10AUTO2)
	{
		if (gSystemMgr.m_bAutoFlag_10) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetTextColor(RGB(0, 255, 0));
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetTextColor(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_60)
	{
		if (gSystemMgr.m_bHomeFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_66)
	{
		if (!gSystemMgr.m_bMotorErrorFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_100)
	{
		if (gSystemMgr.m_nPassRfidFlag) {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	//rfid
	else if (id == IDC_STATIC_97)
	{
		if (gSystemMgr.ScalesConnectFlag)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	//plasma
	else if (id == IDC_STATIC_102)
	{
		if (gSystemMgr.PlasmaConnectFlag)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_109)
	{
		if (gSystemMgr.PassPlasma == 0) //未屏蔽
		{
			if (gSystemMgr.PlasmaSizeup == 1)//开启判断
			{
				if (gSystemMgr.PlasmaPowFlag == 1)
				{
					hbr = CreateSolidBrush(RGB(0, 255, 0));//正常
				}
				else
				{
					hbr = CreateSolidBrush(RGB(255, 0, 0));
				}
			}
			else
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_68)
	{
		if (gSystemMgr.Com1ConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_96)
	{
		if (gSystemMgr.Com2ConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_10STATUS || id == IDC_STATIC_20STATUS2)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"Testing")
		{
			hbr = CreateSolidBrush(RGB(222, 233, 20));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(70, 111, 166));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_10STATUS2)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"OK")
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else if (Cstr == L"...") {
			hbr = CreateSolidBrush(RGB(255, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_10ERROR || id == IDC_STATIC_20ERROR2)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"...")
		{
			hbr = CreateSolidBrush(RGB(157, 187, 210));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else
	{
		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	}
	hbr1 = hbr;
	return hbr1;
}


void CProjectAADlg::OnOpenProjectDlg()
{
	if (m_cProjectDlg != nullptr)
	{
		delete m_cProjectDlg;
		m_cProjectDlg = nullptr;
	}
	if (m_cProjectDlg == nullptr)
	{
		m_cProjectDlg = new CChangeProjectDlg;
		m_cProjectDlg->Create(IDD_DIALOG_PROJECT, this);
	}
	m_cProjectDlg->ShowWindow(SW_SHOW);
}



void CProjectAADlg::OnOpenIODlg()
{
	if (m_cIODlg != nullptr)
	{
		delete m_cIODlg;
		m_cIODlg = nullptr;
	}
	if (m_cIODlg == nullptr)
	{
		m_cIODlg = new CIODlg;
		m_cIODlg->Create(IDD_DIALOG_IO, this);
	}
	m_cIODlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButtonHome()
{
	gSystemLog.DisplayLogAndSave(L"按下回原点按钮", LV_EVENT);
	if (gSystemMgr.m_bHomeFlag == 0)
		gSystemMgr.m_nButtonValue = 10;
}


void CProjectAADlg::OnBnClickedCheck10auto()
{
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_10AUTO);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		if (gSystemMgr.GlueSNCheckFlag == 1) {
			if (m_cGlueSNCheckDlg != nullptr)
			{
				delete m_cGlueSNCheckDlg;
				m_cGlueSNCheckDlg = nullptr;
			}
			if (m_cGlueSNCheckDlg == nullptr)
			{
				m_cGlueSNCheckDlg = new CGlueSNCheckDlg;
				m_cGlueSNCheckDlg->Create(IDD_DIALOG_GLUESNCHECK, this);
			}
			m_cGlueSNCheckDlg->ShowWindow(SW_SHOW);
		}
		else {
			gSystemMgr.m_bAutoFlag_20 = 1;
			gSystemMgr.m_cIo.SetOutputIo(29, ON);
			cstr.Append(L"点胶开启自动");
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			GetDlgItem(IDC_CHECK_10AUTO)->SetWindowTextW(L"点胶");
		}
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_bAutoFlag_20 = 0;
		cstr.Append(L"点胶关闭自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_10AUTO)->SetWindowTextW(L"点胶");
	}
}


void CProjectAADlg::OnOpenControlDlg()
{
	if (m_cControlDlg != nullptr)
	{
		delete m_cControlDlg;
		m_cControlDlg = nullptr;
	}
	if (m_cControlDlg == nullptr)
	{
		m_cControlDlg = new CControlDlg;
		m_cControlDlg->Create(IDD_DIALOG_CONTROL, this);
	}
	m_cControlDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenSettingDlg()
{
	if (m_cSettingDlg != nullptr)
	{
		delete m_cSettingDlg;
		m_cSettingDlg = nullptr;
	}
	if (m_cSettingDlg == nullptr)
	{
		m_cSettingDlg = new CSettingDlg;
		m_cSettingDlg->Create(IDD_DIALOG_SETTING, this);
	}
	m_cSettingDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenRS232Dlg()
{
	if (m_cRS232Dlg != nullptr)
	{
		delete m_cRS232Dlg;
		m_cRS232Dlg = nullptr;
	}
	if (m_cRS232Dlg == nullptr)
	{
		m_cRS232Dlg = new CRS232Dlg;
		m_cRS232Dlg->Create(IDD_DIALOG_RS232, this);
	}
	m_cRS232Dlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedCheck10auto2()
{
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_10AUTO2);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		gSystemMgr.m_bAutoFlag_10 = 1;
		gSystemMgr.m_cIo.SetOutputIo(44, ON);//上线体电机
		if (gSystemMgr.SpeedMode == 1)
			gSystemMgr.m_cIo.SetOutputIo(46, ON);
		else
			gSystemMgr.m_cIo.SetOutputIo(47, ON);
		gSystemMgr.m_cIo.SetOutputIo(37, ON);//下线体电机
		cstr.Append(L"上料开启自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_10AUTO2)->SetWindowTextW(L"上料");
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_bAutoFlag_10 = 0;
		cstr.Append(L"上料关闭自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_10AUTO2)->SetWindowTextW(L"上料");
	}
}



BOOL CProjectAADlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE) return TRUE; //如果是空格消息，返回；
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) return TRUE; //如果是Esc消息，返回；
	if (gSystemMgr.m_nGetCodeFlag == 0 && pMsg->message == WM_KEYDOWN) {
		USES_CONVERSION;
		if (pMsg->wParam == VK_RETURN && gSystemMgr.m_nBarcodeLength > 0)
		{
			gSystemLog.DisplayLogAndSave(A2T(gSystemMgr.Barcode), LV_EVENT);
			SetDlgItemTextW(IDC_STATIC_10SN, A2T(gSystemMgr.Barcode));
			//gSystemMgr.m_nGetCodeFlag = 1;
			if (gSystemMgr.CheckSNFlag == 1) {
				if (gSystemMgr.m_nBarcodeLength == gSystemMgr.CheckSNLength) {
					gSystemMgr.m_nGetCodeFlag = 1;
				}
				else {
					CString cstr;
					cstr.Format(L"PCB码长度错误:[%d],%s", gSystemMgr.m_nBarcodeLength, A2T(gSystemMgr.Barcode));
					gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

					SetDlgItemTextW(IDC_STATIC_10SN, L"...");
					gSystemMgr.m_nGetCodeFlag = 0;
					memset(gSystemMgr.Barcode, 0, sizeof(gSystemMgr.Barcode));
					gSystemMgr.m_nBarcodeLength = 0;
				}
			}
			else
				gSystemMgr.m_nGetCodeFlag = 1;
			return true;
		}

		if (gSystemMgr.m_nBarcodeLength >= 50) {
			gSystemMgr.m_nGetCodeFlag = -1;
			memset(gSystemMgr.Barcode, 0, sizeof(gSystemMgr.Barcode));
			gSystemMgr.m_nBarcodeLength = 0;
			return true;
		}
		switch (pMsg->wParam)
		{
			case VK_A:
			case VK_B:
			case VK_C:
			case VK_D:
			case VK_E:
			case VK_F:
			case VK_G:
			case VK_H:
			case VK_I:
			case VK_J:
			case VK_K:
			case VK_L:
			case VK_M:
			case VK_N:
			case VK_O:
			case VK_P:
			case VK_Q:
			case VK_R:
			case VK_S:
			case VK_T:
			case VK_U:
			case VK_V:
			case VK_W:
			case VK_X:
			case VK_Y:
			case VK_Z:
			case VK_0:
			case VK_1:
			case VK_2:
			case VK_3:
			case VK_4:
			case VK_5:
			case VK_6:
			case VK_7:
			case VK_8:
			case VK_9:
				gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
				gSystemMgr.m_nBarcodeLength++;
				break;
		}
		//gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
		//gSystemMgr.m_nBarcodeLength++;
		if (gSystemMgr.m_nBarcodeLength > 0)
		{
			SetDlgItemTextW(IDC_STATIC_10SN, A2T(gSystemMgr.Barcode));
		}
	}

	if (pMsg->message == WM_KEYDOWN /*&& pMsg->wParam == VK_RETURN*/) return TRUE;

	return CDialogEx::PreTranslateMessage(pMsg);
}




void CProjectAADlg::OnBnClickedButtonHome4()
{
	if (gSystemMgr.m_nPassRfidFlag) {
		if (IDYES == AfxMessageBox(L"确定 开启RFID 吗？", MB_YESNO | MB_ICONWARNING))
		{
			gSystemMgr.m_nPassRfidFlag = 0;
		}
	}
	else {
		if (IDYES == AfxMessageBox(L"确定 屏蔽RFID 吗？", MB_YESNO | MB_ICONWARNING))
		{
			gSystemMgr.m_nPassRfidFlag = 1;
		}
	}
}


void CProjectAADlg::OnBnClickedButton1()
{
	SetDlgItemTextW(IDC_STATIC_10SN, L"...");
	gSystemMgr.m_nGetCodeFlag = 0;
	memset(gSystemMgr.Barcode, 0, sizeof(gSystemMgr.Barcode));
	gSystemMgr.m_nBarcodeLength = 0;
}


void CProjectAADlg::OnBnClickedButtonHome5()
{
	if (m_cUserCheckDlg != nullptr)
	{
		delete m_cUserCheckDlg;
		m_cUserCheckDlg = nullptr;
	}
	if (m_cUserCheckDlg == nullptr)
	{
		m_cUserCheckDlg = new CUserCheckDlg;
		m_cUserCheckDlg->Create(IDD_DIALOG_USER, this);
	}
	m_cUserCheckDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenLoginDlg()
{
	if (m_cLoginDlg != nullptr)
	{
		m_cLoginDlg->ShowWindow(SW_SHOW);
	}
	if (m_cLoginDlg == nullptr)
	{
		m_cLoginDlg = new CLoginDlg;
		m_cLoginDlg->Create(IDD_DIALOG_LOGIN, this);
	}
	m_cLoginDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenCaptureDlg()
{
	if (m_cCaptureDlg != nullptr)
	{
		delete m_cCaptureDlg;
		m_cCaptureDlg = nullptr;
	}
	if (m_cCaptureDlg == nullptr)
	{
		m_cCaptureDlg = new CCaptureDlg;
		m_cCaptureDlg->Create(IDD_DIALOG_CAPTURE, this);
	}
	m_cCaptureDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButtonStart()
{
	gSystemMgr.m_bStopCapture = 0;
	CWnd* pBtn = nullptr;
	pBtn = (CWnd*)GetDlgItem(IDC_BUTTON_START);
	pBtn->EnableWindow(FALSE);
}


void CProjectAADlg::OnBnClickedButtonStop()
{
	gSystemMgr.m_bStopCapture = 1;
	gSystemMgr.imgCap->m_bInitFlag = FALSE;
	CWnd* pBtn = nullptr;
	pBtn = (CWnd*)GetDlgItem(IDC_BUTTON_START);
	pBtn->EnableWindow(TRUE);
}


void CProjectAADlg::OnBnClickedButtonSave()
{
	gSystemMgr.SaveTestReport_White(gSystemMgr.imgCap->bBmpTestBuf, gSystemMgr.imgCap->iBmpW, gSystemMgr.imgCap->iBmpH, 0);
}


void CProjectAADlg::OnBnClickedButton2()
{
	gSystemMgr.m_nButtonValue = 25;
}


void CProjectAADlg::OnBnClickedButton3()
{
	if (IDYES == AfxMessageBox(L"确定 清零Plasma喷头使用次数 吗？", MB_YESNO | MB_ICONWARNING))
	{
		CString cstr;
		gSystemMgr.PlasmaLife = 0;
		cstr.Format(L"%d", gSystemMgr.PlasmaLife); gSystemMgr.m_cPosConfigIni.SaveConfigIni(L"PosConfig", L"PlasmaLife", cstr);
	}
}


void CProjectAADlg::OnBnClickedButton4()
{
	if (IDYES == AfxMessageBox(L"确定 探针使用次数 吗？", MB_YESNO | MB_ICONWARNING))
	{
		CString cstr;
		gSystemMgr.TanzhenLife = 0;
		cstr.Format(L"%d", gSystemMgr.TanzhenLife); gSystemMgr.m_cPosConfigIni.SaveConfigIni(L"PosConfig", L"TanzhenLife", cstr);
	}
}


void CProjectAADlg::OnOpenGlueCheckDlg()
{
	if (m_cGlueCheckDlg != nullptr)
	{
		m_cGlueCheckDlg->ShowWindow(SW_SHOW);
	}
	if (m_cGlueCheckDlg == nullptr)
	{
		m_cGlueCheckDlg = new CGlueCheckDlg;
		m_cGlueCheckDlg->Create(IDD_DIALOG_GLUE_CHECK, this);
	}
	m_cGlueCheckDlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::OnMenuMes()
{
	if (m_cMesDlg != nullptr)
	{
		m_cMesDlg->ShowWindow(SW_SHOW);
	}
	if (m_cMesDlg == nullptr)
	{
		m_cMesDlg = new CMesDlg;
		m_cMesDlg->Create(IDD_MES, this);
	}
	m_cMesDlg->ShowWindow(SW_SHOW);
}
